package com.hongning.railway.ui.main.cadremanager;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.ReqCadreListBean;
import com.hongning.railway.entity.resbean.BaseResCadreListBean;
import com.hongning.railway.entity.resbean.ResCadreBean;
import com.hongning.railway.ui.main.cadremanager.itemviewmodel.CadreManagerItemViewModel;
import com.hongning.railway.util.Apputils;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * Author: zhangjing
 * DATA:2019-08-29 10:54
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class CadreManagerFragmentViewModel extends BaseViewModel<AppRepository> {
    public ItemBinding<CadreManagerItemViewModel> item = ItemBinding.of(BR.viewModel, R.layout.item_cadre);
    public ObservableList<CadreManagerItemViewModel> viewlist= new ObservableArrayList();

    public CadreManagerFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }


    public void loadData(){
        ReqCadreListBean reqCadreListBean = new ReqCadreListBean();
        reqCadreListBean.setStaffId(Apputils.getStaffid());
        model.getCadreList(reqCadreListBean)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .subscribe(new ApiDisposableObserver<BaseResCadreListBean<ResCadreBean>>() {
                    @Override
                    public void onResult(BaseResCadreListBean<ResCadreBean> resCadreBeanBaseResCadreListBean) {
                        for (ResCadreBean item:resCadreBeanBaseResCadreListBean.getData()) {
                            viewlist.add(new CadreManagerItemViewModel(CadreManagerFragmentViewModel.this,item));
                        }
                    }
                });
    }
}
